//
//  GetOrderListVm.swift
//  Pods
//
//  Created by eme on 2017/3/28.
//
//

import SwiftyJSON
import RxSwift
import Result
import NetWorkCore
import ModelProtocol
import UtilCore

class GetOrderListVm:Base_Vm{
    
    let backDate = Variable<[TheOrderListModel]>([])
    //    let disposeBag = DisposeBag()
    //    let error = PublishSubject<MikerError>()
    
    let refreshing = Variable(false)
    
    let page = Variable(1)
    
    let hasNextPage = Variable(true)
    
    var loading : Observable<Bool>
    
    let orderStatus = Variable("")
    
    var orderStat = ""
    
    var evaluate = ""
    
    init (
        
        input:(
        refreshTriger:Observable<Void>,
        loadMoreTriger:Observable<Void>
        )
        ){
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        super.init()
        
        let refreshRequest = Observable.of(
            input.refreshTriger,
            reloadTrigger.withLatestFrom(input.refreshTriger)
            ).merge().map{ _ in
                
                return MineApi.getOrderList(page: 0, length: 10, orderType: self.orderStat ,isevaluate:self.evaluate)
                
            }.shareReplay(1)
        
        
        
        let refreshResponse = refreshRequest.emeRequestApiForArray(TheOrderListModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        
        refreshResponse.subscribe(onNext:{[unowned self] result in
            switch result{
            case .success(let data):
                
                self.hasNextPage.value = data.count == 10
                self.backDate.value = data
                
            case .failure(let error):
                
                self.refresherror.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        
        let loadMoreRequest = (
            input.loadMoreTriger
            ).map{ _ in
                return MineApi.getOrderList(page: self.page.value - 1, length: 10, orderType: self.orderStat,isevaluate:self.evaluate)
                
            }.shareReplay(1)
        
        let loadMoreResponse = loadMoreRequest.emeRequestApiForArray(TheOrderListModel.self).shareReplay(1)
        
        loadMoreResponse.subscribe(onNext:{[unowned self] result in
            switch result {
            case .success(let data):
                
                self.hasNextPage.value = data.count == 10
                self.backDate.value += data
                self.page.value += 1
                
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        refreshRequest.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 2
            
            }).addDisposableTo(disposeBag)
        
        
        let request = Observable.of(
            orderStatus.asObservable(),
            reloadTrigger.withLatestFrom(orderStatus.asObservable())
            ).merge().skip(1)
            .map{ type in
                
                return MineApi.getOrderList(page: 0, length: 10, orderType: type,isevaluate:self.evaluate)
                
            }.shareReplay(1)
        
        let response = request.emeRequestApiForArray(TheOrderListModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] result in
            switch result{
            case.success(let data):
                
                self.hasNextPage.value = data.count == 10
                self.backDate.value = data
                
            case .failure(let error):
                self.refresherror.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        request.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 2
            
        }).addDisposableTo(disposeBag)
        
    }
    
}
